/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package servlet;

import java.io.IOException;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import viviendas.Conexion;

public class Contratos extends Conexion {

    private String id_contrato;
    private String id_vivienda;
    private String ganancia;

    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        HttpSession session = request.getSession();
        try {
            if (request.getParameter("id_contrato") != null) {
                String accion = request.getParameter("accion");
                int id_contrato = Integer.parseInt(request.getParameter("id_contrato"));
                int id_vivienda = Integer.parseInt(request.getParameter("id_vivienda"));
                int id_usuario = Integer.parseInt(request.getParameter("id_usuario"));
                String telefono = request.getParameter("telefono");
                String direccion = request.getParameter("direccion");
                String dui = request.getParameter("dui");
                String ganancia = request.getParameter("ganancia");
                String descripcion_contrato = request.getParameter("descripcion_contrato");

                String usuario = request.getParameter("usuario");
                String clave = request.getParameter("clave");
                if (accion.equalsIgnoreCase("insertar")) {
                    save("insert into contratos (id_vivienda, id_usuario, telefono, direccion, dui, ganancia, descripcion_contrato) values("
                          
                            + "" + id_vivienda + ""
                            + ", " + id_usuario + ""
                            + ", '" + telefono + "'"
                            + ", '" + direccion + "'"
                            + ", '" + dui + "'"
                            + ", " + ganancia + ""
                            + ", '" + descripcion_contrato + "'"
                            + ")");
                    setSuccessMessage("Contrato creado exitosamente.");
                } else if (accion.equalsIgnoreCase("actualizar")) {
                    update("update contrato set "
                            + "id_contrato = " + id_contrato + ""
                            + "id_vivienda = " + id_vivienda + ""
                            + ", id_usuario = " + id_usuario + ""
                            + ", telefono = '" + telefono + "'"
                            + ", direccion = '" + direccion + "'"
                            + ", dui= '" + dui + "'"
                            + ", ganancia = " + ganancia + ""
                            + ", descripcion_contrato = '" + descripcion_contrato + "'"
                            + " where id_contrato = " + id_contrato);
                    setSuccessMessage("Contrato actualizado exitosamente.");
                } else if (accion.equalsIgnoreCase("Borrar")) {
                    delete("delete from contratos where id_contrato= " + id_contrato);
                    setSuccessMessage("Contrato eliminado exitosamente.");
                }
            }
        } catch (SQLException ex) {
            System.out.println("Error en mantenimiento contrato " + ex.getMessage());
        } finally {
            session.setAttribute("successMessage", getSuccessMessage());
            session.setAttribute("errorMessage", getErrorMessage());
            setSuccessMessage("");
            setErrorMessage("");
            response.sendRedirect("contrato.jsp");
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
